﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.ToolsLib;
using xiaoxiong.KYJDAL.Models;
using xiaoxiong.KYJDAL.MyDbcontext;
using xiaoxiong.KYJDAL.Services;
using xiaoxiong.ScadaProModbusRtu.Common;
using xiaoxiong.ScadaProModbusRtu.Message.MessageForms;

namespace xiaoxiong.ScadaProModbusRtu
{
    public partial class FrmLogin : Form
    {
        SysAdminService service = new SysAdminService();

        //窗体拖动事件
        private bool _isDragging = false;
        private Point _startPoint = new Point(0, 0);
        //创建配置路径
        private string configPath = Application.StartupPath + @"\config\config.ini";
        public FrmLogin()
        {
            InitializeComponent();

            //设置路径
            IniConfigHelper.IniPath = configPath;

            this.Load += FrmLogin_Load;

        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            //读取系统配置
            CommonMethods.GetSysConfig();

            if (CommonMethods.sysConfig != null)
            {
                if (CommonMethods.sysConfig.AutoLogin)
                {
                    btn_Login_Click(null, null);
                }
            }
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
        private void btn_Login_Click(object sender, EventArgs e)
        {

            if (txt_UserName.Text.Trim().Length == 0 || txt_Pwd.Text.Trim().Length == 0)
            {
                new FrmMessageBoxWithoutAck("请输入账号或密码","登录提示").ShowDialog();
                return;
            }
            SysAdmin sysAdmin = new SysAdmin()
            {
                LoginName = txt_UserName.Text,
                LoginPwd = txt_Pwd.Text,
            };

            var loginRes = service.Login(sysAdmin);
            if (loginRes != null)
            {
                CommonMethods.CommonAdmin = loginRes;
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                new FrmMessageBoxWithoutAck("账号或密码错误", "登录提示").ShowDialog();
                return;
            }
        }

        private void btn_Login_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Enter))
            {
                btn_Login_Click(null, null);
            }
        }

        private void FrmLogin_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _isDragging = true;
                _startPoint = new Point(e.X, e.Y);
            }
        }

        private void FrmLogin_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isDragging)
            {
                Point newPoint = this.PointToScreen(new Point(e.X, e.Y));
                this.Location = new Point(newPoint.X - _startPoint.X, newPoint.Y - _startPoint.Y);
            }
        }

        private void FrmLogin_MouseUp(object sender, MouseEventArgs e)
        {
            _isDragging = false;
        }
    }
}
